package com.cumt.threeway.fragment;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.cumt.threeway.R;
import com.cumt.threeway.activity.ActivitiesStrategyActivity;
import com.cumt.threeway.activity.HotActivitiesActivity;
import com.cumt.threeway.view.widget.SelectPicPopupWindow;

/**
 * Created by Jingjing.Hu on 2016/4/1.
 */
public class HomePageFragment extends Fragment implements View.OnClickListener{
    LinearLayout mDanren;
    LinearLayout mJieban;
    LinearLayout mJiating;
    LinearLayout mQnglv;
    LinearLayout mGonglue;
    LinearLayout mJijin;
    View mShouyeLayout;
    CallSetTabSelection selection;
    // 自定义的弹出框类
    SelectPicPopupWindow menuWindow;
    private LocationManagerProxy aMapManager;

    public static HomePageFragment newInstance() {
        return new HomePageFragment();
    }

    //回调接口
    public interface CallSetTabSelection{
        public abstract void callSetTab(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mShouyeLayout = inflater.inflate(R.layout.fragment_homepage_layout, null,false);
        initView();
        startAmap();
        return mShouyeLayout;
    }



    private void initView() {
        mDanren = (LinearLayout) mShouyeLayout.findViewById(R.id.lineardanren);
        mJieban = (LinearLayout) mShouyeLayout.findViewById(R.id.linearjieban);
        mJiating = (LinearLayout) mShouyeLayout.findViewById(R.id.linearjiating);
        mQnglv = (LinearLayout) mShouyeLayout.findViewById(R.id.linearqinglv);
        mGonglue = (LinearLayout) mShouyeLayout.findViewById(R.id.lineargonglue);
        mJijin = (LinearLayout) mShouyeLayout.findViewById(R.id.linearjijin);//猜你喜欢
        mShouyeLayout.findViewById(R.id.iv_contact_us).setOnClickListener(this);

        mDanren.setOnClickListener(this);
        mJijin.setOnClickListener(this);//猜你喜欢
        mJieban.setOnClickListener(this);
        mJiating.setOnClickListener(this);
        mQnglv.setOnClickListener(this);
        mGonglue.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lineardanren:
                if(selection != null) {
                    selection.callSetTab(0);
                }
                break;
            case R.id.linearjieban:
                if(selection != null) {
                    selection.callSetTab(1);
                }
                break;
            case R.id.linearqinglv:
                if(selection != null) {
                    selection.callSetTab(2);
                }
                break;
            case R.id.linearjiating:
                if(selection != null) {
                    selection.callSetTab(3);
                }
                break;
            case R.id.lineargonglue:
                goStrategyActivity();
                break;
            case R.id.linearjijin:
                goHotActivity();
                break;
            case R.id.iv_contact_us:
                menuWindow = new SelectPicPopupWindow(getActivity(),itemsOnClick);
                // 显示窗口
                menuWindow.showAtLocation(getActivity().findViewById(R.id.content),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
                break;
            default:
                break;
        }
    }

    private void goHotActivity() {
        Intent intent = new Intent(getActivity(), HotActivitiesActivity.class);
        startActivity(intent);
    }

    private void goStrategyActivity() {
        Intent intent = new Intent(getActivity(), ActivitiesStrategyActivity.class);
        startActivity(intent);
    }

    // 为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_pick_photo:
                    // 关联电话号码
                    Uri uri = Uri.parse("tel:18052128441");
                    Intent it = new Intent(Intent.ACTION_DIAL, uri);
                    // 启动打电话软件
                    startActivity(it);
                default:
                    break;
            }
        }

    };

    public void setSelection(CallSetTabSelection selection) {
        this.selection = selection;
    }

    private void startAmap() {
        aMapManager = LocationManagerProxy.getInstance(getActivity());
        aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork,
                2000, 10, mAMapLocationListener);
    }

    private AMapLocationListener mAMapLocationListener = new AMapLocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onLocationChanged(Location location) {

        }

        @Override
        public void onLocationChanged(AMapLocation location) {
            if (location != null) {
                TextView textView = (TextView) getActivity().findViewById(R.id.tv_location);
                textView.setText(location.getCity());
            }
        }
    };
}
